﻿using QD.LopPlat.UI.Code.DbContent;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QD.Tools;
using QD.LopPlat.UI.Model.Sys;
using System.Data.Entity.Validation;
using QD.LopPlat.UI.Code;

namespace QD.LopPlat.UI
{
    public partial class Sys_UidPwdQfMag : BaseForm
    {
        private int qfId = 0;

        public Sys_UidPwdQfMag()
        {
            InitializeComponent();

            base.ToolsBtnUpdate = this.ToolsBtnUpdate;
            base.ToolsBtnClose = this.toolStripButton2;

            this.grid_uid.InitGrid();
            this.grid_uid.RowTemplate.ContextMenuStrip = this.contextMenuStrip1;
        }

        private void Sys_UidPwdMag_Load(object sender, EventArgs e)
        {
            Bind_UidQf();

            this.lbQfCode.Text = QDDataBaseHelper.CurQDDataBase.QfCode;
            this.lbQfName.Text = QDDataBaseHelper.CurQDDataBase.QfName;
        }

        private void Bind_UidQf()
        {
            DbQfDevPlat db = new DbQfDevPlat();
            string sql = string.Format("select a.*,b.UserName as UName from Sys_UidRefQf a left join Sys_UidPwd b on a.Uid=b.Uid  where a.QfCode='{0}'", QDDataBaseHelper.CurQDDataBase.QfCode);

            string _key = this.txtKey.Text.Trim();
            if (!string.IsNullOrEmpty(_key))
            {
                sql += string.Format(" and (a.Uid like '%{0}%' or b.UserName like '%{0}%')", _key);
            }
            DataTable dt = db.Query(sql);

            this.grid_uid.DataSource = dt;

            //修正默认修中第一行的问题
            if (dt.Rows.Count > 0)
            {
                this.grid_uid.Rows[0].Selected = false;
            }
        }

        protected override void ToolsBtnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string uid = this.Uid.Text.Trim();
                string username = this.UserName.Text.Trim();

                DbQfDevPlat dbPlat = new DbQfDevPlat();
                if (qfId == 0)
                {
                    throw new Exception("请先选择需要设置的用户");
                }

                Sys_UidRefQf mod = dbPlat.Sys_UidRefQf.First(p => p.ID == qfId);
                if (mod == null)
                {
                    throw new Exception("被设置的用户不存在或已经被删除！");
                }
                mod.SysManager = this.SysManager.Checked;
                mod.IsEnableDesign = this.IsEnableDesign.Checked;

                dbPlat.SaveChanges();

                PromptHelper.Prompt("保存成功！");

                InitUIData(qfId);
            }
            catch (DbEntityValidationException ex)
            {
                GeneralTools.EFValidaPrompt(ex);
            }
            catch (Exception ex)
            {
                PromptHelper.Prompt(ex);
            }
        }

        private void InitUIData(int id)
        {
            DbQfDevPlat dbPlat = new DbQfDevPlat();

            Sys_UidRefQf mod = dbPlat.Sys_UidRefQf.First(p => p.ID == id && p.QfCode == QDDataBaseHelper.CurQDDataBase.QfCode);

            Sys_UidPwd u_mod = dbPlat.Sys_UidPwd.First(p => p.Uid == mod.Uid);

            this.Uid.Text = mod.Uid;
            this.UserName.Text = u_mod.UserName;
            this.SysManager.Checked = mod.SysManager;
            this.IsEnableDesign.Checked = mod.IsEnableDesign;

            this.qfId = id;
        }

        private void 复制单元格ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.grid_uid.CopyFirstSelectedCellValue();
        }

        private void 复制所有选中单元格ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.grid_uid.CopySelectedCellsValue();
        }

        private void grid_uid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                int _id = Convert.ToInt32(this.grid_uid.Rows[e.RowIndex].Cells["qfListID"].Value);
                InitUIData(_id);
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Sys_UidPwdQfAdd frm = new Sys_UidPwdQfAdd();
            frm.SureEvent += Frm_SureEvent;
            frm.ShowDialog();
        }

        private void Frm_SureEvent(string[] userlist)
        {
            try
            {
                DbQfDevPlat dbPlat = new DbQfDevPlat();

                foreach (string user in userlist)
                {
                    if (!dbPlat.Sys_UidRefQf.Any(p => p.Uid == user && p.QfCode == QDDataBaseHelper.CurQDDataBase.QfCode))
                    {
                        Sys_UidRefQf mod = new Sys_UidRefQf();
                        mod.QfCode = QDDataBaseHelper.CurQDDataBase.QfCode;
                        mod.Uid = user;
                        mod.IsEnableDesign = false;
                        mod.CreateDate = DateTime.Now;
                        dbPlat.Sys_UidRefQf.Add(mod);
                    }
                }

                dbPlat.SaveChanges();
                Bind_UidQf();
            }
            catch (Exception ex)
            {
                Tools.PromptHelper.Prompt(ex);
            }
        }

        private void txtKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Bind_UidQf();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (PromptHelper.PromptOkCancel("您确认将选中用户移除当前应用？") == DialogResult.OK)
            {
                try
                {
                    IList<DataGridViewRow> rows = this.grid_uid.GetSelectRows();

                    if (rows.Count == 0)
                    {
                        throw new Exception("你未选择任何需要移除的用户！");
                    }
                    else
                    {
                        DbQfDevPlat dbPlat = new DbQfDevPlat();
                        foreach (DataGridViewRow row in rows)
                        {
                            int id = Convert.ToInt32(row.Cells["qfListID"].Value);
                            Sys_UidRefQf mod = dbPlat.Sys_UidRefQf.FirstOrDefault(p => p.ID == id);
                            if (mod != null)
                            {
                                dbPlat.Sys_UidRefQf.Remove(mod);
                            }
                        }
                        dbPlat.SaveChanges();
                        qfId = 0;
                        Bind_UidQf();

                        PromptHelper.Prompt("移除成功！");
                    }
                }
                catch (Exception ex)
                {
                    PromptHelper.Prompt(ex);
                }
            }
        }
    }
}
